package org.geektimes.web.mvc.header;

import org.geektimes.base.util.TypeUtils;
import org.geektimes.web.mvc.header.matedata.ParamInfo;
import org.geektimes.web.mvc.header.matedata.WebContent;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 抽象的参数解析器  
* @author : KangNing Hu
*/
public abstract class AbstractParamParse implements ParamParse {

	@Override
	public Object parse(WebContent webContent, ParamInfo paramInfo) {
		ServletRequest servletRequest= webContent.getRequest();
		ServletResponse servletResponse = webContent.getResponse();
		if (!(servletRequest instanceof HttpServletRequest) || !(servletResponse instanceof HttpServletResponse)){
			throw new IllegalStateException("暂不支持非http/https以为的协议");
		}
		return parse0(webContent , paramInfo);
	}


	/**
	 * 转换为http
	 * @param servletRequest servlet请求对象
	 * @return
	 */
	protected HttpServletRequest toHttp(ServletRequest servletRequest){
		return (HttpServletRequest) servletRequest;
	}


	/**
	 * 转换为http
	 * @param servletResponse servlet响应对象
	 * @return
	 */
	protected HttpServletResponse toHttp(ServletResponse servletResponse){
		return (HttpServletResponse) servletResponse;
	}



	protected abstract Object parse0(WebContent webContent, ParamInfo paramInfo );

	/**
	 * 类型转换
	 * @param value 待转换值
	 * @param type  转换最终类型
	 * @return 转换后的值
	 */
	public Object transType(String value , Class type){
		return TypeUtils.transform(value , type);
	}

	public static void main(String[] args) {
		System.out.println(int.class == Integer.class);
	}
}
